/**
* ServerThread
* 
* @author Safran Quader
* @version: 0.1
*/

package controller;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Set;

import model.ManagementAssistent;
import model.Ort;
import model.Produkt;
import model.Trainer;

public class ServerThread extends Thread{
	
	ManagementAssistent ma = new ManagementAssistent(1, "Test", "Admin", "Max");
	
	private Socket s = null;
	private ObjectInputStream ois = null;
	private ObjectOutputStream oos = null;
	
	public ServerThread(Socket s){
		this.s = s;
	}
	
	 public void closeAll(  ) {
	        try {
	            if( ois != null ) {
	                ois.close(  );
	            }

	            if( oos != null ) {
	                oos.close(  );
	            }

	            if( s != null ) {
	                s.close(  );
	            }
	        } catch( IOException e ) {
	            System.out.println( e );
	        }

	        s = null;
	        ois = null;
	        oos = null;
	    }
	
	Object insertData(Object object){
		DatabaseController db = new DatabaseController(ma);
		db.connect();
		db.insertTraining((model.Training) object);
		db.close();
		return object;
	}
	
	Object selectProduct(){
		DatabaseController db = new DatabaseController(ma);
		db.connect();
		
		Sendobject sendobject = new Sendobject("", 0, 2, "String[] Produkt", null);
		
		Set<Produkt> products = db.getProduktList();
		
		db.close();
		
		String[] productString = new String[products.size()];
		
		int i = 0;
		
		for(Produkt s: products){
			productString[i] = s.getProduktID() + " - " + s.getBezeichnung();
			i++;	
		}
		
		sendobject.setObject(productString);
		
		return sendobject;
	}
	
	Object selectPlace(){
		DatabaseController db = new DatabaseController(ma);
		db.connect();
		
		Sendobject sendobject = new Sendobject("", 0, 2, "String[] Ort", null);
		
		Set<Ort> places = db.getOrtList();
		
		db.close();
		
		String[] placeString = new String[places.size()];
		
		int i = 0;
		
		for(Ort s: places){
			placeString[i] = s.getOrtsID() + " - " + s.getGebaeude() + " - " + s.getOrtsID();
			i++;	
		}
		
		sendobject.setObject(placeString);
		
		return sendobject;
	}
	
	Object selectTrainer(){
		System.out.println("selectTrainer - start");
		
		DatabaseController db = new DatabaseController(ma);
		db.connect();
		
		Sendobject sendobject = new Sendobject("", 0, 2, "String[] Trainer", null);
		
		Set<Trainer> trainers = db.getTrainerList();
		
		db.close();
		
		String[] trainerString = new String[trainers.size()];
		
		int i = 0;
		
		for(Trainer s: trainers){
			trainerString[i] = s.getTrainerID() + " - " + s.getName() + ", " + s.getVorname();
			i++;	
		}
		
		sendobject.setObject(trainerString);
		
		System.out.println("String[] trainer selektiert");
		
		return sendobject;
	}
	
	Object selectComment(Produkt product){
		String bemerkung = product.getBemerkungen();
		return bemerkung;
	}
	
	public void run(){
		
		int command = 0;
		String art = null;
		Object object = null;
		
		try{
			ois = new ObjectInputStream(s.getInputStream());
//			System.out.println("ObjectInputStream erstellt");
			//ObjectOutputStream oos = new ObjectOutputStream( s.getOutputStream());
			//TO-DO Abfrage
			Sendobject obj = (Sendobject) ois.readObject();
//			System.out.println("Sendobject extrahiert");
			
			command = obj.getCommand();
			art = obj.getArt();
			object = obj.getObject();
			
			switch(command){
				case 1:
					//Insert
					insertData(object);
					break;
				case 2:
					// Select
					if(art.equals("String[] Produkt")){
						object = selectProduct();
					}
					if(art.equals("String[] Trainer")){
						object = selectTrainer();
					}
					if(art.equals("String[] Ort")){
						object = selectPlace();
					}
					if(art.equals("String Bemerkung")){
						object = selectComment((Produkt) object);
					}
					if(art.equals("String Beschreibung")){
//						object = selectDescription();
					}
					break;
				case 3:
					//Update
					break;
				case 4:
					//delete
					break;
				default:
					;
			}
			
//			obj.setArt(");
			
			oos = new ObjectOutputStream(s.getOutputStream());
			oos.writeObject(object);
			
			//os.println( "" );
            oos.flush(  );
            
			ois.close();
			System.out.println("Objekt gesendet");
		}
		catch (IOException e){
			e.printStackTrace();
		}
		catch (ClassNotFoundException e){
			e.printStackTrace();
		}
		closeAll();
	}
}
